<?php
namespace app\api\service;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class Upload extends Controller {

    /**
     * 图片上传接口
     * @return mixed
     */
    public static function upload_image() {
        // 获取表单上传文件 例如上传了001.jpg
        try {
            $file = request()->file('image');

            $qiniu = self::upload_qiniu($file);
            if ($qiniu['err'] !== null) {
                return ["err"=>1,"msg"=>$qiniu['err'],"data"=>""];
            } else {
                return ['code' => 200, 'data' => ['path' => $qiniu['key']], 'msg' => 'success'];
            }
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 多图片上传接口
     * @return mixed
     */
    public static function upload_images() {
        // 获取表单上传文件 例如上传了001.jpg
        try {
            $files = request()->file('image');
            $images = [];
            foreach ($files as $file) {
                $qiniu = self::upload_qiniu($file);
                if ($qiniu['err'] !== null) {
                    throw new \Exception($qiniu['err']);
//                    return ["err"=>1,"msg"=>$err,"data"=>""];
                } else {
                    $images[] = $qiniu['key'];
                    //return ['code' => 200, 'data' => ['path' => $domain . $ret['key']], 'msg' => 'success'];
                }
            }
            return ['code' => 200, 'data' => $images, 'msg' => 'success'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }

    /**
     * layim图片上传
     * @return mixed
     */
    public static function layim_upload_img() {
        // 获取表单上传文件 例如上传了001.jpg
        try {
            $file = request()->file('file');

            $qiniu = self::upload_qiniu($file);
            if ($qiniu['err'] !== null) {
                return ["err"=>1,"msg"=>$qiniu['err'],"data"=>""];
            } else {
                return ['code' => 200, 'path' => $qiniu['key'], 'msg' => 'success'];
            }
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }

    public static function upload_qiniu($file) {
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = config('qiniu.ak');
        $secretKey = config('qiniu.sk');
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = config('qiniu.bucket');
        $domain = config('qiniu.domain');
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        return [
            'err' => $err,
            'key' => $domain.$ret['key']
        ];
    }
}